
//泛型
function echo<T>(arg: T): T {
    return arg
}

const result1 = echo(123)
const result2 = echo('123')

function swap<T, P>(tuple: [T, P]): [P, T] {
    return [tuple[1], tuple[0]]
}
const result3 = swap(["123", 123])
result3[1]

//约束泛型
interface IWithLength {
    length: number
}
function echoWithLength<T extends IWithLength>(arg: T): T {
    // console.log(arg.length)
    return arg
}

const str1 = echoWithLength('str')
const obj1 = echoWithLength({ length: 10 })
const arr2 = echoWithLength([1, 2.3, 4])

//队列类
class Queue<T> {
    private data = [];
    push(item: T) {
        return this.data.push(item)
    };
    pop(): T {
        return this.data.shift()
    }
}

const queue = new Queue<number>()
queue.push(1)
console.log(queue.pop().toFixed())

const queue2 = new Queue<string>()
queue2.push('str')
console.log(queue2.pop().length)

//接口泛型
interface KeyPair<T, U> {
    Key: T;
    value: U;
}

let kp1: KeyPair<number, string> = { Key: 1, value: 'str' }
let kp2: KeyPair<string, number> = { Key: 'str', value: 1 }

let arr: number[] = [1, 2, 4]
let arrtwo: Array<number> = [1, 2, 3]

//函数泛型

interface IPlus<T> {
    (a: T, b: T): T
}

function plus(a: number, b: number) {
    return a + b
}
const a: IPlus<number> = plus

function pluss(a: string, b: string) {
    return a + b
}
const b: IPlus<string> = pluss




